package com.hemaapp.yjnh.activity;

import xtom.frame.XtomConfig;
import xtom.frame.util.Md5Util;
import xtom.frame.util.XtomSharedPreferencesUtil;
import xtom.frame.util.XtomToastUtil;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.yjnh.view.BaseButtonDialog;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.SysInitInfo;

public class PwdresetActivity extends BaseActivity implements OnClickListener {

	private ImageButton left;
	private TextView title;
	private Button titleRight;

	private EditText oldpwd;
	private EditText newpwd;
	private EditText newpwd_confirm;
	private TextView tv_hint;
    private TextView tvReset;

	String oldpwdStr="";
	String newpwdStr="";
	String newpwdCon="";
	private int type = 0;// 1:修改登录密码，2:修改支付密码

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_pwdreset);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case PASSWORD_SAVE:
			showProgressDialog("请稍候");
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case PASSWORD_SAVE:
			cancelProgressDialog();
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case PASSWORD_SAVE:
			XtomToastUtil.showLongToast(mContext, "密码修改成功");
			String keytype = netTask.getParams().get("keytype");
			if (keytype.equals("1"))//如果是修改登录密码，那么需要保存最新的登录密码
				XtomSharedPreferencesUtil.save(mContext, "password", Md5Util.getMd5(XtomConfig.DATAKEY + Md5Util.getMd5(newpwdStr)));
			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					finish();
				}
			}, 1000);
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case PASSWORD_SAVE:
			XtomToastUtil.showLongToast(mContext, "密码修改失败，"+baseResult.getMsg());
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case PASSWORD_SAVE:
			XtomToastUtil.showLongToast(mContext, "密码修改失败");
			break;
		}
	}

	@Override
	protected void findView() {
		left = (ImageButton) findViewById(R.id.title_left_btn);
		title = (TextView) findViewById(R.id.title_text);
		titleRight = (Button) findViewById(R.id.title_right_btn);
		
		oldpwd = (EditText) findViewById(R.id.oldpwd);
		newpwd = (EditText) findViewById(R.id.newpwd);
		newpwd_confirm = (EditText) findViewById(R.id.newpwd_confirm);
		tv_hint = (TextView) findViewById(R.id.tv_hint);
        tvReset = (TextView) findViewById(R.id.tv_reset);
	}

	@Override
	protected void getExras() {
		type = getIntent().getIntExtra("type", 0);
	}

	@Override
	protected void setListener() {
		left.setOnClickListener(this);
		if (type == 2)
		    title.setText("修改提现/支付密码");
        else
            title.setText("修改密码");
		titleRight.setText("提交");
		titleRight.setOnClickListener(this);
		if (type == 1) {
			tv_hint.setVisibility(View.INVISIBLE);
		}else if (type == 2) {
			tv_hint.setVisibility(View.VISIBLE);
            tvReset.setVisibility(View.VISIBLE);
//			SysInitInfo info = getApplicationContext().getSysInitInfo();
//			tv_hint.setText("初始密码：123456 \n如忘记密码请致电客服："+info.getSys_service_phone());
//			tv_hint.setText("初始密码：123456 \n忘记提现/支付密码？");
		}
        tvReset.setOnClickListener(this);
	}

	private BaseButtonDialog hbDialog;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_left_btn:
			finish();
			break;
		case R.id.title_right_btn:
			oldpwdStr = oldpwd.getText().toString().trim();
			newpwdStr = newpwd.getText().toString().trim();
			newpwdCon = newpwd_confirm.getText().toString().trim();

			if (isNull(oldpwdStr)) {
				showTextDialog("请输入原密码");
				break;
			}
			if (isNull(newpwdStr)) {
				showTextDialog("请输入新密码");
				break;
			}
			if (isNull(newpwdCon)) {
				showTextDialog("请再次输入新密码");
				break;
			}
			if (newpwdStr.length()<6 || newpwdStr.length()>16) {
				showTextDialog("密码输入不正确，请输入6-16位密码");
				break;
			}
			if (!newpwdStr.equals(newpwdCon)) {
				showTextDialog("两次新密码输入不一致");
				break;
			}
			if (newpwdStr.equals(oldpwdStr)) {
				showTextDialog("新密码不能与旧密码一致");
				break;
			}
			//支付密码和提现密码的原密码判断交给服务器
			getNetWorker().passwordSave(
					getApplicationContext().getUser().getToken(), type+"", oldpwdStr, newpwdStr);
			break;
		case R.id.tv_reset:
            Intent it = new Intent(mContext, GetPassword0Activity.class);
            it.putExtra("keytype", "2");
            startActivity(it);
			break;
		}
	}

}
